package com.shop.config;

import com.shop.exception.BusinessException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ModelAndView handleBusinessException(BusinessException e, HttpServletRequest request) {
        log.error("业务异常：{}", e.getMessage());
        ModelAndView mv = new ModelAndView();
        mv.addObject("error", e.getMessage());
        mv.addObject("url", request.getRequestURL());
        mv.setViewName("error/business");
        return mv;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception e, HttpServletRequest request) {
        log.error("系统异常：", e);
        ModelAndView mv = new ModelAndView();
        mv.addObject("error", "系统异常，请联系管理员");
        mv.addObject("url", request.getRequestURL());
        mv.setViewName("error/500");
        return mv;
    }
} 